﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Besture.Bll;
using Besture.Model;
using Besture.Common;

namespace Besture.UI.Portal.Home
{
    public partial class Index : System.Web.UI.Page
    {
        public static string Img_Path = "/Resource/images/";
        //Constants.PATH_IMAGE_CONTENT
        //15021531042 123qwe

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 加载首页酒店图片
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string GetHomeHotels()
        {
            string json = "";
            int wHotel = 0;
            StringBuilder sb = new StringBuilder();
            HotelsDac dac = new HotelsDac();
            IList<Index_Hotel_Model> list = dac.GetHomeHotels();
            IList<Index_Hotel_Model> list1 = list.Where(p => p.Type == 1).ToList();

            if (list1.Count >= 4)
            {
                for (int i = 0; i < 4; i++)
                {
                    sb.Append("{");
                    sb.Append("\"ID\":" + list1[i].HotelID);
                    sb.Append(",");
                    sb.Append("\"HotelsName\":" + "\"" + list1[i].HotelsName + "\"");
                    sb.Append(",");
                    if (!string.IsNullOrEmpty(list1[i].HotelsImage))
                    {
                        sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list1[i].HotelsImage + "\"");
                    }
                    else
                    {
                        sb.Append("\"HotelsImage\":" + "\"" + Img_Path + "DefaultImg.jpg\"");
                    }
                    sb.Append(",");
                    sb.Append("\"Introduction\":" + "\"" + (list1[i].Introduction.Length > 10 ? list1[i].Introduction.Substring(0, 10).Replace("\r", " ").Replace("\n", " ") + "..." : list1[i].Introduction.Replace("\r", " ").Replace("\n", " ")) + "\"");
                    sb.Append(",");
                    sb.Append("\"Area\":" + "\"" + list1[i].Area + "\"");
                    sb.Append("},");
                }
            }
            else
            {
                for (int i = 0; i < list1.Count; i++)
                {
                    sb.Append("{");
                    sb.Append("\"ID\":" + list1[i].HotelID);
                    sb.Append(",");
                    sb.Append("\"HotelsName\":" + "\"" + list1[i].HotelsName + "\"");
                    sb.Append(",");
                    if (!string.IsNullOrEmpty(list1[i].HotelsImage))
                    {
                        sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list1[i].HotelsImage + "\"");
                    }
                    else
                    {
                        sb.Append("\"HotelsImage\":" + "\"" + Img_Path + "DefaultImg.jpg\"");
                    }
                    sb.Append(",");
                    sb.Append("\"Introduction\":" + "\"" + (list1[i].Introduction.Length > 10 ? list1[i].Introduction.Substring(0, 10).Replace("\r", " ").Replace("\n", " ") + "..." : list1[i].Introduction.Replace("\r", " ").Replace("\n", " ")) + "\"");
                    sb.Append(",");
                    sb.Append("\"Area\":" + "\"" + list1[i].Area + "\"");
                    sb.Append("},");
                }

                for (int i = 0; i < 4 - list1.Count; i++)
                {
                    sb.Append(GetEmptyImg());
                }
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();

            Index_Hotel_Model m2 = list.Where(p => p.Type == 2 && p.WidthImageUrl != null).FirstOrDefault();
            if (m2 != null)
            {
                wHotel = m2.HotelID;
                sb.Append("{");
                sb.Append("\"ID\":" + m2.HotelID);
                sb.Append(",");
                sb.Append("\"HotelsName\":" + "\"" + m2.HotelsName + "\"");
                sb.Append(",");
                if (!string.IsNullOrEmpty(m2.WidthImageUrl))
                {
                    sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + m2.WidthImageUrl + "\"");
                }
                else
                {
                    sb.Append("\"HotelsImage\":" + "\"" + Img_Path + "DefaultImg.jpg\"");
                }
                sb.Append(",");
                sb.Append("\"Introduction\":" + "\"" + (m2.Introduction.Length > 10 ? m2.Introduction.Substring(0, 10).Replace("\r", " ").Replace("\n", " ") + "..." : m2.Introduction.Replace("\r", " ").Replace("\n", " ")) + "\"");
                sb.Append(",");
                sb.Append("\"Area\":" + "\"" + m2.Area + "\"");
                sb.Append("},");
            }
            else
            {
                IList<Index_Hotel_Model> list2 = list.Where(p => p.Type == 2).ToList();
                if (list2.Count > 0)
                {
                    wHotel = list2[0].HotelID;
                    sb.Append("{");
                    sb.Append("\"ID\":" + list2[0].HotelID);
                    sb.Append(",");
                    sb.Append("\"HotelsName\":" + "\"" + list2[0].HotelsName + "\"");
                    sb.Append(",");
                    if (!string.IsNullOrEmpty(list2[0].WidthImageUrl))
                    {
                        sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list2[0].WidthImageUrl + "\"");
                    }
                    else
                    {
                        sb.Append("\"HotelsImage\":" + "\"" + Img_Path + "DefaultImg.jpg\"");
                    }
                    sb.Append(",");
                    sb.Append("\"Introduction\":" + "\"" + (list2[0].Introduction.Length > 10 ? list2[0].Introduction.Substring(0, 10).Replace("\r", " ").Replace("\n", " ") + "..." : list2[0].Introduction.Replace("\r", " ").Replace("\n", " ")) + "\"");
                    sb.Append(",");
                    sb.Append("\"Area\":" + "\"" + list2[0].Area + "\"");
                    sb.Append("},");
                }
                else
                {
                    sb.Append(GetEmptyImg());
                }
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();

            IList<Index_Hotel_Model> list3 = list.Where(p => p.Type == 2 && p.HotelID != wHotel).ToList();
            if (list3.Count > 0)
            {
                sb.Append("{");
                sb.Append("\"ID\":" + list3[0].HotelID);
                sb.Append(",");
                sb.Append("\"HotelsName\":" + "\"" + list3[0].HotelsName + "\"");
                sb.Append(",");
                if (!string.IsNullOrEmpty(list3[0].HotelsImage))
                {
                    sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list3[0].HotelsImage + "\"");
                }
                else
                {
                    sb.Append("\"HotelsImage\":" + "\"" + Img_Path + "DefaultImg.jpg\"");
                }
                sb.Append(",");
                sb.Append("\"Introduction\":" + "\"" + (list3[0].Introduction.Length > 10 ? list3[0].Introduction.Substring(0, 10).Replace("\r", " ").Replace("\n", " ") + "..." : list3[0].Introduction.Replace("\r", " ").Replace("\n", " ")) + "\"");
                sb.Append(",");
                sb.Append("\"Area\":" + "\"" + list3[0].Area + "\"");
                sb.Append("},");
                json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
                sb.Clear();

                if (list3.Count >= 4)
                {
                    for (int i = 1; i < 4; i++)
                    {
                        sb.Append("{");
                        sb.Append("\"ID\":" + list3[i].HotelID);
                        sb.Append(",");
                        sb.Append("\"HotelsName\":" + "\"" + list3[i].HotelsName + "\"");
                        sb.Append(",");
                        if (!string.IsNullOrEmpty(list3[i].HotelsImage))
                        {
                            sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list3[i].HotelsImage + "\"");
                        }
                        else
                        {
                            sb.Append("\"HotelsImage\":" + "\"" + Img_Path + "DefaultImg.jpg\"");
                        }
                        sb.Append(",");
                        sb.Append("\"Introduction\":" + "\"" + (list3[i].Introduction.Length > 10 ? list3[i].Introduction.Substring(0, 10).Replace("\r", " ").Replace("\n", " ") + "..." : list3[i].Introduction.Replace("\r", " ").Replace("\n", " ")) + "\"");
                        sb.Append(",");
                        sb.Append("\"Area\":" + "\"" + list3[i].Area + "\"");
                        sb.Append("},");
                    }
                }
                else
                {
                    for (int i = 1; i < list3.Count; i++)
                    {
                        sb.Append("{");
                        sb.Append("\"ID\":" + list3[i].HotelID);
                        sb.Append(",");
                        sb.Append("\"HotelsName\":" + "\"" + list3[i].HotelsName + "\"");
                        sb.Append(",");
                        if (!string.IsNullOrEmpty(list3[i].HotelsImage))
                        {
                            sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list3[i].HotelsImage + "\"");
                        }
                        else
                        {
                            sb.Append("\"HotelsImage\":" + "\"" + Img_Path + "DefaultImg.jpg\"");
                        }
                        sb.Append(",");
                        sb.Append("\"Introduction\":" + "\"" + (list3[i].Introduction.Length > 10 ? list3[i].Introduction.Substring(0, 10).Replace("\r", " ").Replace("\n", " ") + "..." : list3[i].Introduction.Replace("\r", " ").Replace("\n", " ")) + "\"");
                        sb.Append(",");
                        sb.Append("\"Area\":" + "\"" + list3[i].Area + "\"");
                        sb.Append("},");
                    }

                    for (int i = 0; i < 3 - (list3.Count-1); i++)
                    {
                        sb.Append(GetEmptyImg());
                    }
                }
                json += "{\"items\":[" + sb.ToString().Trim(',') + "]}";
            }
            else
            {
                json += "{\"items\":[" + sb.Append(GetEmptyImg()) +"]},";
                sb.Clear();
                for (int i = 0; i < 3; i++)
                {
                    sb.Append(GetEmptyImg());
                }
                json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            }
            
            return "[" + json.Trim(',') + "]";
        }

        /// <summary>
        /// 暂无图片
        /// </summary>
        /// <returns></returns>
        private static string GetEmptyImg()
        {
            return "{\"ID\":0,\"HotelsName\":\"暂无图片\",\"HotelsImage\":" + "\"" + Img_Path + "DefaultImg.jpg\",\"Introduction\":\"暂无图片\",\"Area\":\"暂无图片\"},";
        }

        ///// <summary>
        ///// 加载首页酒店图片
        ///// </summary>
        ///// <returns></returns>
        //[WebMethod]
        //public static string GetHomeHotels()
        //{
        //    string json = "";
        //    StringBuilder sb = new StringBuilder();
        //    HotelsDac dac = new HotelsDac();
        //    IList<Index_Hotel_Model> list = dac.GetHomeHotels();
        //    IList<Index_Hotel_Model> list1 = list.Where(p => p.Type == 1).ToList();
        //    if (list1.Count > 0)
        //    {
        //        for (int i = 0; i < list1.Count; i++)
        //        {
        //            if (i < 4)
        //            {
        //                sb.Append("{");
        //                sb.Append("\"ID\":" + list1[i].HotelID);
        //                sb.Append(",");
        //                sb.Append("\"HotelsName\":" + "\"" + list1[i].HotelsName + "\"");
        //                sb.Append(",");
        //                sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list1[i].HotelsImage + "\"");
        //                sb.Append(",");
        //                sb.Append("\"Introduction\":" + "\"" + (list1[i].Introduction.Length > 10 ? list1[i].Introduction.Substring(0, 10) + "..." : list1[i].Introduction) + "\"");
        //                sb.Append(",");
        //                sb.Append("\"Area\":" + "\"" + list1[i].Area + "\"");
        //                sb.Append("},");
        //            }
        //            else
        //            {
        //                break;
        //            }
        //        }
        //    }
        //    json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
        //    sb.Clear();
        //    IList<Index_Hotel_Model> list2 = list.Where(p => p.Type == 2 && p.WidthImageUrl != null).ToList();
        //    if (list2.Count > 0)
        //    {
        //        sb.Append("{");
        //        sb.Append("\"ID\":" + list2[0].HotelID);
        //        sb.Append(",");
        //        sb.Append("\"HotelsName\":" + "\"" + list2[0].HotelsName + "\"");
        //        sb.Append(",");
        //        sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list2[0].WidthImageUrl + "\"");
        //        sb.Append(",");
        //        sb.Append("\"Introduction\":" + "\"" + (list2[0].Introduction.Length > 10 ? list2[0].Introduction.Substring(0, 10) + "..." : list2[0].Introduction) + "\"");
        //        sb.Append(",");
        //        sb.Append("\"Area\":" + "\"" + list2[0].Area + "\"");
        //        sb.Append("},");
        //    }
        //    json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
        //    sb.Clear();
        //    IList<Index_Hotel_Model> list3 = list.Where(p => p.Type == 2 && p.WidthImageUrl == null).ToList();
        //    if (list3.Count > 0)
        //    {
        //        sb.Append("{");
        //        sb.Append("\"ID\":" + list3[0].HotelID);
        //        sb.Append(",");
        //        sb.Append("\"HotelsName\":" + "\"" + list3[0].HotelsName + "\"");
        //        sb.Append(",");
        //        sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list3[0].HotelsImage + "\"");
        //        sb.Append(",");
        //        sb.Append("\"Introduction\":" + "\"" + (list3[0].Introduction.Length > 10 ? list3[0].Introduction.Substring(0, 10) + "..." : list3[0].Introduction) + "\"");
        //        sb.Append(",");
        //        sb.Append("\"Area\":" + "\"" + list3[0].Area + "\"");
        //        sb.Append("},");
        //        json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
        //        sb.Clear();

        //        for (int i = 1; i < list3.Count; i++)
        //        {
        //            if (i < 4)
        //            {
        //                sb.Append("{");
        //                sb.Append("\"ID\":" + list3[i].HotelID);
        //                sb.Append(",");
        //                sb.Append("\"HotelsName\":" + "\"" + list3[i].HotelsName + "\"");
        //                sb.Append(",");
        //                sb.Append("\"HotelsImage\":" + "\"" + Constants.PATH_IMAGE_CONTENT + list3[i].HotelsImage + "\"");
        //                sb.Append(",");
        //                sb.Append("\"Introduction\":" + "\"" + (list3[i].Introduction.Length > 10 ? list3[i].Introduction.Substring(0, 10) + "..." : list3[i].Introduction) + "\"");
        //                sb.Append(",");
        //                sb.Append("\"Area\":" + "\"" + list3[i].Area + "\"");
        //                sb.Append("},");
        //            }
        //            else
        //            {
        //                break;
        //            }
        //        }
        //        json += "{\"items\":[" + sb.ToString().Trim(',') + "]}";
        //    }
        //    else
        //    {
        //        json += "{\"items\":[]},{\"items\":[]}";
        //    }

        //    return "[" + json + "]";
        //}
    }
}